﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;

namespace Homework
{
    public partial class FormMain : Form
    {
        //说明游戏对象 游戏类
        private Game game;

        public FormMain()
        {
            InitializeComponent();
        }

        private void panelRight_Paint(object sender, PaintEventArgs e)
        {
            if (game!= null)
            {
                game.Drawright(e.Graphics);
            }
        }

        private void userControlNoBlink2_Load(object sender, EventArgs e)
        {

        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //初始化游戏对象 游戏类
            game = new Game();
            //初始化时钟
            game.gameChanged += Game_gameChanged;
            game.gameOver += Game_gameOver;
        }

        private void Game_gameOver()
        {
            if (game.Herodead!=0)
            {
                String message = "累计得分:"+game.Enemydead.ToString();
                MessageBox.Show(message,"游戏结束");
                game = new Game();
            }
            
        }
        private void Game_gameChanged()
        {
            if (game!=null)
            {
                //如果时钟开始 则发生游戏区域重绘事件
                GameArea.Invalidate();
                panelRight.Invalidate();
            }
        }

        private void GameArea_Paint(object sender, PaintEventArgs e)
        {
            if (game!=null)//游戏已经初始化了
            {
                game.Draw(e.Graphics, this.GameArea.Size);
            }
        }

        private void FormMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (game!=null)
            {
                //调用游戏类中的键盘事件
                 game.KeyDown(e.KeyCode.ToString());                
                //重绘游戏对象 玩家飞机
                GameArea.Invalidate();
            }
        }

        private void buttonlife_Click(object sender, EventArgs e)
        {
            game.ButtonAdd();
        }

        //重置游戏
        private void buttonstart_Click(object sender, EventArgs e)
        {
            //弹出窗体
            NewGame formNG = new NewGame();
            formNG.ShowDialog();
            //初始化游戏类
            game = new Game();
            game.gameChanged += Game_gameChangednew;


        }

        //重置时钟
        private void Game_gameChangednew()
        {
            this.GameArea.Invalidate();
        }

        private void buttonstop_Click(object sender, EventArgs e)
        {
            game.Timer.Enabled = !game.Timer.Enabled;
        }

        private void buttonfrozen_Click(object sender, EventArgs e)
        {
            game.ButtonFrozen();
        }

        private void buttonaboutme_Click(object sender, EventArgs e)
        {
            //点击弹出窗口
            FormAboutMe formAM = new FormAboutMe();
            formAM.ShowDialog();
        }

        private void axWindowsMediaPlayerbackground_Enter(object sender, EventArgs e)
        {
            if (game==new Game())
            {
                //开始播放
                MucisBackground.Ctlcontrols.play();
            }
        }
    }
}
